<?php
/**
 * Created by PhpStorm.
 * Date: 2018/4/23
 * Time: 下午10:32
 * qq: 1067713962
 */
namespace app\home\controller;
use think\Db;
class Jiaoyi extends Com
{
    public function index()
    {
        $price = db('bijia')->value('price');
        //查询该用户交易订单
        $result=db('jiaoyidingdan')->where('uid','=',session('user_id'))->order('id DESC')->select();
        $this->assign('result',$result);
        $this->assign('price',$price);
        return view();

    }
    public function zhuanhua(){
        if($_POST){
          //检查用户余额
            $id=session('user_id');
            if(!$id){
                $message="请先登陆！";
                $json=json_encode($_POST);
                return $json;
            }
            $money=db('mumber')->where('uid','=',$id)->value('mymoney');

            if($money==0||$money<$_POST['data']){
               $message="您的币数不足";
               echo $message;
               return;
            }
            //查询当前币价
            $bijia=db('bijia')->value('price');
            $zongjia=$bijia*$_POST['data'];
            //币数转化佣金，生成订单
            if($zongjia!=0){
                $row = [
                    'uid' => $id,
                    'shuliang' => $_POST['data'],
                    'price'=>$money,
                    'time'=>date('Y-m-d H:i:s', time()),
                    'zongjia'=>$zongjia
                ];
                //用户余额减少
               $val= Db::name('mumber')->where('uid','=',$id)->setDec('mymoney',$_POST['data']);


               if(!$val){
                   $message="您的币数不足";
                   echo $message;
                   return;
               }
                $result=Db::name('jiaoyidingdan')->insert($row);
                if(!$result){
                    $message="发生未知错误";
                    echo $message;
                    return;
                }else{
                    $message="挂单成功";
                    echo $message;
                    return;
                }



            }
        }
    }
    public function jump()
    {
        return view();
    }
}
